package com.yunc.modules.shop.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunc.modules.shop.entity.ShopItemSpecs;
import com.yunc.modules.shop.mapper.ShopItemSpecsMapper;
import com.yunc.modules.shop.service.IShopItemSpecsService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商品规格库存
 *
 * @author congcong tao
 * @create 2018/12/23 10:16 PM
 */
@Service("shopItemSpecsService")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ShopItemSpecsServiceImpl extends ServiceImpl<ShopItemSpecsMapper, ShopItemSpecs> implements IShopItemSpecsService {

    private final ShopItemSpecsMapper shopItemSpecsMapper;

    @Override
    public boolean saveSpecs(String itemId, String itemSpecsJson) {
        // 移除之前的
        QueryWrapper<ShopItemSpecs> ew = new QueryWrapper<>();
        ew.eq("item_id", itemId);
        this.remove(ew);
        List<ShopItemSpecs> itemSpecsList = JSONObject.parseArray(itemSpecsJson, ShopItemSpecs.class);
        if (itemSpecsList != null && itemSpecsList.size() > 0) {
            int orderNum = 1;
            for (ShopItemSpecs itemSpecs : itemSpecsList) {
                itemSpecs.setItemId(itemId);
                itemSpecs.setOrderNum(orderNum);
                this.save(itemSpecs);
                orderNum++;
            }
        }
        return true;
    }

    @Override
    public ShopItemSpecs getLockItemSpecs(String id) {
        return shopItemSpecsMapper.getLockItemSpecs(id);
    }
}
